//
//  FindViewController.m
//  WWLNetease
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "FindViewController.h"
#import "WZXFindTableViewCell.h"
#import "WZXFindModel.h"
#import "WZXFindDetailViewController.h"

@interface FindViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong)UITableView *myTableView;

@property (nonatomic ,strong)NSMutableArray *allArray;

@property (nonatomic ,assign)NSInteger pageNumber;

///判断上拉还是下拉
@property (nonatomic ,assign)BOOL upLoad;

@end

@implementation FindViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)loadView {
    [super loadView];
#pragma mark - 初始化allArray
    self.allArray = [NSMutableArray array];
    
    self.navigationItem.title = @"问吧";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 46) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
#pragma mark - 注册cell
    [self.myTableView registerClass:[WZXFindTableViewCell class] forCellReuseIdentifier:@"cell"];
//    //调用请求数据的方法
//    [self getData];
    [self addHeaderReflash];
    [self.myTableView.mj_header beginRefreshing];
    [self addFooterReflash];

}

#pragma mark - 下拉刷新
- (void)addHeaderReflash {
    self.upLoad = YES;
    self.pageNumber = 0;
    [self.myTableView.mj_header beginRefreshing];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.myTableView.mj_header = header;
    
}
#pragma mark - 下拉加载
- (void)addFooterReflash {
    
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        self.upLoad = NO;
        self.pageNumber = self.pageNumber + 10;
        [self getData];
    }];
    
}

#pragma mark - 解析数据
- (void)getData {
    
    
    NSString *urlStr =[NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/%ld-10.html",self.pageNumber];
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        if (self.upLoad == YES) {
            
            [self.allArray removeAllObjects];
        }
        
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        NSMutableArray *expertListArr = [dataDic objectForKey:@"expertList"];
        for (NSDictionary *dic in expertListArr) {
            
            WZXFindModel *model = [[WZXFindModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
           
            [self.allArray addObject:model];
        }
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"请求失败");
    }];
  
}




#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WZXFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    WZXFindModel *model = [self.allArray objectAtIndex:indexPath.row];
    
    cell.findModel = model;
    
    return cell;
    
}

#pragma mark - 必要的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allArray.count;
}

#pragma mark - 高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 345 * SIZE;
}

#pragma mark - 点击方式
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //取消点击至灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WZXFindDetailViewController *findDetailVC = [[WZXFindDetailViewController alloc] init];
    
    WZXFindModel *findModel = [self.allArray objectAtIndex:indexPath.row];
    
    findDetailVC.expertID = findModel.expertId;
    
    [self.navigationController pushViewController:findDetailVC animated:YES];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
